﻿Public Class man_movimientos

    'poarametros para mantenedor ql
    Dim tituloVentna As String = "Mantenedor de movimientos"
    Dim columnas() As String = {"id-0", "Usuario-130", "Rut usuario-150", "Movimiento-150", "Seccion afectada-130", "Fecha ocurrencia-130"} 'separar por - numero:largo
    Dim nombreTabla As String = "registro_movimiento"
    Dim idTabla As String = "rm_id"
    Dim qryOriginal = "select rm_id,(usr_nombre+ ' '+usr_apellido)as nombre,usr_rut,mov_descripcion,rm_seccion,rm_fecha from REGISTRO_MOVIMIENTO inner join USUARIOS on USUARIOS.USR_ID=REGISTRO_MOVIMIENTO.USR_ID inner join MOVIMIENTO on MOVIMIENTO.MOV_ID=REGISTRO_MOVIMIENTO.MOV_ID"
    Dim condicion As String
    'NO MODIFICAR
    Dim qry As String = qryOriginal
    Dim cadena As String = qry
    Dim ultima_qry As String
    Dim row As Integer
    Dim id As String
    Private Sub btn_cerrar_Click(sender As System.Object, e As System.EventArgs)
        Me.Close()
    End Sub
    Public Sub actualizar()
        carga_lista(qryOriginal)
    End Sub
    Public Sub carga_columnas(ByVal columnas() As String)
        Dim col() As String
        ListView1.View = View.Details
        For Each cols As String In columnas
            col = Split(cols, "-")
            ListView1.Columns.Add(col(0), Integer.Parse(col(1)), HorizontalAlignment.Left)
        Next
    End Sub

    Public Sub carga_lista(ByVal qry)
        ListView1.Items.Clear()
        ultima_qry = qry 'aca dejo la ultima ejecutada :D
        Dim dataset As New DataSet
        Consulta(qry, dataset)
        If dataset.Tables("data").Rows.Count = 0 Then
            ' MsgBox("No hay Registros", MsgBoxStyle.Information, "Error")
        Else
            With dataset.Tables("data")
                For f As Integer = 0 To .Rows.Count - 1
                    Dim dato As New ListViewItem(.Rows(f).Item(0).ToString)
                    ' recorrer las columnas  
                    For c As Integer = 1 To .Columns.Count - 1
                        dato.SubItems.Add(.Rows(f).Item(c).ToString())
                    Next

                    Select Case dataset.Tables("data").Rows(f)("mov_descripcion")
                        Case "Eliminar"
                            ListView1.Items.Add(dato).ForeColor = Color.Red
                        Case "Modificar"
                            ListView1.Items.Add(dato).ForeColor = Color.Orange
                        Case "Agregar"
                            ListView1.Items.Add(dato).ForeColor = Color.DarkGreen
                        Case "Acceder"
                            ListView1.Items.Add(dato).ForeColor = Color.Blue
                        Case "Seleccionar"
                            ListView1.Items.Add(dato).ForeColor = Color.Aqua
                        Case "Exportar"
                            ListView1.Items.Add(dato).ForeColor = Color.DarkOliveGreen

                    End Select



                Next
            End With

        End If
    End Sub
    Public Sub inicia_mantenedor()
        With Me
            .Icon = (My.Resources.favicon)
            .Text = tituloVentna
        End With
        ' Me.Icon = (My.Resources.favicon)
        carga_columnas(columnas)
        carga_lista(qryOriginal & " order by rm_fecha desc")
        'carga_combo(comboItems)
    End Sub
    Public Sub eliminar(ByRef id As String)
        qry = "delete from " & nombreTabla & " where " & idTabla & "='" & id & "'"
        If MsgBox("¿Esta seguro de eliminar este registro?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Try
                EjecutaConsulta(qry)
                'registro movimiento
                registro("eliminar", login.usuario.id, main.modulo.movimiento)
                actualizar()
            Catch ex As Exception
                MsgBox("Error en DB")
            End Try
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btn_cerrar.Click
        Me.Close()
    End Sub

    Private Sub man_estudios_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        inicia_mantenedor()
        RadioButton4.Checked = True
    End Sub
    Private Sub vigentes_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles vigentes.CheckedChanged
        condicion = " where mov_descripcion='Agregar'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub todos_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles todos.CheckedChanged
        condicion = " where mov_descripcion='Eliminar'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub terminados_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles terminados.CheckedChanged
        qry = cadena & " where mov_descripcion='Modificar'"
        carga_lista(qry)
    End Sub

    Private Sub btn_exportar_Click(sender As System.Object, e As System.EventArgs) Handles btn_exportar.Click
        Dim dataset As New DataSet
        Consulta(ultima_qry, dataset)
        exel(dataset, ProgressBar1)
        'registro movimiento
        registro("exportar", login.usuario.id, main.modulo.movimiento)
    End Sub

    Private Sub btn_imprimir_Click(sender As System.Object, e As System.EventArgs) Handles btn_imprimir.Click
        reporte.Owner = Me
        Dim rp As New rpt_estudio
        reporte.carga_info(ultima_qry, rp)
    End Sub

    Private Sub btn_actualizar_Click(sender As System.Object, e As System.EventArgs) Handles btn_actualizar.Click
        actualizar()
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
        condicion = " where mov_descripcion='Acceder'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
        condicion = " where mov_descripcion='Seleccionar'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton3.CheckedChanged
        condicion = " where mov_descripcion='Exportar'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton4_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton4.CheckedChanged
        actualizar()
    End Sub

    Private Sub btn_eliminar_Click(sender As System.Object, e As System.EventArgs) Handles btn_eliminar.Click
        Try
            row = ListView1.SelectedIndices.Item(0)
            id = ListView1.Items.Item(row).SubItems(0).Text
            eliminar(id)
        Catch
            MsgBox("Debe seleccionar un registro")
        End Try

    End Sub

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If MsgBox("¿Desea Eliminar todos los movimientos?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If RadioButton4.Checked = True Then
                EjecutaConsulta("delete from registro_movimiento")
                'registro movimiento
                registro("eliminar", login.usuario.id, main.modulo.movimiento)
                actualizar()
            Else
                EjecutaConsulta("delete from registro_movimiento where mov_id=(select mov_id from movimiento" & condicion & ")")
                'registro movimiento
                registro("eliminar", login.usuario.id, main.modulo.movimiento)
                actualizar()
            End If
        End If


    End Sub
End Class